//
//  NavTipsView.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/21.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "NavTipsView.h"

#define kAnimationTime 0.5


@interface NavTipsView()


@property (nonatomic, weak)UILabel *textLbl;
@property (nonatomic, weak)UIImageView *tipIcon;
@property (nonatomic, assign) NavTipsViewType bannerType;
@property (nonatomic, assign)BOOL isExistNav;

@property (nonatomic , strong) UITapGestureRecognizer *tap;


@end

@implementation NavTipsView



static CGFloat bannerH;
static CGFloat bannerY;
static CGFloat bannerMargin;
static CGFloat statusBarH;

+ (NavTipsView *)defaultTipsView
{
    static NavTipsView *tipsView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tipsView = [[NavTipsView alloc] init];
    });
    return tipsView;
}


- (void)showTipsBannerInView:(UIView *)view WithType:(NavTipsViewType)type text:(NSString *)text isExistNav:(BOOL)isNav
{
    if (self.subviews.count)return;
    self.bannerType = type;
    self.isExistNav = isNav;
    if (isPhoneX) {//X
        if (isNav) {
            bannerH = 50.0f;
            bannerY = 0.0f;
            bannerMargin = 0.0f;
        }else{
            bannerH = 50.0f + 30.0f;
            bannerY = 0.0f;
            bannerMargin = 30.0f;
        }
    }else{//非X
        if (isNav) {
            bannerH = 50.0f;
            bannerY = 0.0f;
            bannerMargin = 0.0f;
        }else{
            bannerH = 50.0f;
            bannerY = 0.0f;
            bannerMargin = 0.0f;
        }
    }
    
    self.backgroundColor = NumberColor(255, 255, 255);
    self.frame = CGRectMake(0, -bannerH, LKScreenW, bannerH);
    [view addSubview:self];
    
    [self textLbl];
    [self tipIcon];
    
    self.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.layer.borderWidth = 0.5;
    self.textLbl.text = text;
    
    [UIView animateWithDuration:kAnimationTime
                          delay:0
         usingSpringWithDamping:0.5f
          initialSpringVelocity:20.0f
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         CGRect rect = self.frame;
                         rect.origin.y  += bannerH;
                         self.frame = rect;
                     }
                     completion:^(BOOL finished) {
                         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                             [self dissmsiss];
                         });
                         
                     }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeAlert)];
    tap.numberOfTapsRequired = 1;
    self.tap = tap;
    [self addGestureRecognizer:tap];
    
}

- (void)showAlwaysInView:(UIView *)view text:(NSString *)text
{
    if (self.subviews)
    {
        [self removeFromSuperview];
    }
    self.bannerType = NavTipsViewTypeMessage;
    
    bannerH = 50.0f;
    bannerY = 0.0f;
    bannerMargin = 0.0f;
    statusBarH = isPhoneX ? 44.0f : 20.0f;
    
    
    self.backgroundColor = NumberColor(255, 255, 255);
    self.frame = CGRectMake(0, -bannerH, LKScreenW, bannerH);
    [view addSubview:self];
    
    [self textLbl];
    [self tipIcon];
    
    self.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.layer.borderWidth = 0.5;
    self.textLbl.text = text;
    
    [UIView animateWithDuration:kAnimationTime
                          delay:0
         usingSpringWithDamping:0.5f
          initialSpringVelocity:20.0f
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         CGRect rect = self.frame;
                         
                         rect.origin.y  += (bannerH + statusBarH);
                         self.frame = rect;
                     }
                     completion:^(BOOL finished) {
                         
                         
                     }];
}

- (void)hide
{
    [UIView animateWithDuration:(kAnimationTime * 2)
                          delay:0
         usingSpringWithDamping:0.8f
          initialSpringVelocity:10.0f
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         CGRect rect = self.frame;
                         rect.origin.y -= (bannerH + statusBarH);
                         self.frame = rect;
                     }
                     completion:^(BOOL finished) {
                         for (UIView *view in self.subviews)
                         {
                             [view removeFromSuperview];
                         }
                         [self removeFromSuperview];
                         
                     }];
}

-(void)removeAlert
{
    [self dissmsiss];
}

- (void)dissmsiss
{
    
    [UIView animateWithDuration:(kAnimationTime * 2)
                          delay:0
         usingSpringWithDamping:0.8f
          initialSpringVelocity:10.0f
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         CGRect rect = self.frame;
                         rect.origin.y -= bannerH;
                         self.frame = rect;
                     }
                     completion:^(BOOL finished) {
                         [self.tap removeTarget:self action:@selector(removeAlert)];
                         self.tap = nil;
                         for (UIView *view in self.subviews)
                         {
                             [view removeFromSuperview];
                         }
                         [self removeFromSuperview];
                         
                     }];
}

-(UILabel *)textLbl
{
    if (_textLbl == nil) {
        UILabel *textLbl = [UILabel new];
        [self addSubview:textLbl];
        textLbl.textColor = NumberColor(226, 96, 66);
        textLbl.font = [UIFont systemFontOfSize:font18ScreenWRate * LKScreenW];
        textLbl.textAlignment = NSTextAlignmentLeft;
        textLbl.numberOfLines = 1;
        textLbl.adjustsFontSizeToFitWidth = YES;
        [textLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self);
            make.right.equalTo(self).offset(-10);
            make.left.equalTo(self).offset(rate375(45));
            make.top.equalTo(self).offset(bannerMargin);
        }];
        _textLbl = textLbl;
    }
    return _textLbl;
}


-(UIImageView *)tipIcon
{
    if (_tipIcon == nil) {
        UIImageView *tipIcon = [UIImageView new];
        [self addSubview:tipIcon];
        NSString *imageName;
        switch (self.bannerType) {
            case NavTipsViewTypeSuccess:
                imageName = @"fankui-queren";
                break;
            case NavTipsViewTypeMessage:
                imageName = @"fankui-tixing";
                break;
                break;
            default:
                break;
        }
        tipIcon.image = [UIImage imageNamed:imageName];
        [tipIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.width.equalTo(@(rate375(16)));
            make.left.equalTo(self).offset(rate375(25));
            make.centerY.equalTo(self.textLbl.mas_centerY);
        }];
        _tipIcon = tipIcon;
        
    }
    return _tipIcon;
}






@end
